package io.renren.modules.app.message.handler;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import io.renren.modules.app.message.MessageResult;
import io.renren.modules.app.message.MsgType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author :
 * @version V1.0
 * @Project:
 * @Package
 * @Description: 商品消息处理器
 * @date
 **/
@Component(ProductMessageHandler.BeanName)
@Slf4j
public class ProductMessageHandler extends AbstractJdVopMessageHandler {
//    @Autowired
//    private PmsProductServiceImpl pmsProductService;
//    @Autowired
//    private PmsProductValidateDataServiceImpl pmsProductValidateDataService;
//    @Autowired
//    private JdVopProductSoldOutHandler jdVopProductSoldOutHandler;
    /**
     *   商品上下架状态改变消息条数
     */
    private static int STATE=0;
    /**
     * 商品详情消息条数
     */
    private static int DETAIL=0;
    /**
     * 商品消息总条数
     */
    private static int COUNT=0;


    public static final String BeanName = "MessageProductHandle";

    private ThreadPoolExecutor productExecutor= new ThreadPoolExecutor(2,10, 20, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>(200));

    public ProductMessageHandler(ApplicationContext applicationContext) {
        super(applicationContext);
        setBeanName(BeanName);
        productExecutor.allowCoreThreadTimeOut(true);
    }


    @Override
    public void read(MessageResult msg) {
//        int type = msg.getType();
        COUNT=++COUNT;
        log.info("商品详情消息条数============{}=====时间：{}======",COUNT,new Date());
//        switch (type) {
//            case MsgType.Product.STATE:
//                productExecutor.execute(() -> this.handleProductState(msg));
//                break;
//            case MsgType.Product.DETAIL:
//                productExecutor.execute(() -> this.handleProductDetail(msg));
//                break;
//            default:
//                log.info("JD_VOP NOT FOUND FUN：{}", msg);
//                notifyMsgReadEvent(msg, false);
//        }
    }


    /**
     * 更新商品状态并推送删除消息
     * @param msg
     */
    public void handleProductState(MessageResult msg) {
        STATE=++STATE;
        log.info("商品上下架状态改变消息条数============{}=====时间：{}======",STATE,new Date());
//        String skuId = getMsg(msg, MessageResult.skuId);
//        Date date=msg.getTime();
////        log.info("1,msg={}",msg);
//        //验证是否已经记录
//        PmsProductValidateData validateData=this.pmsProductValidateDataService.findOneBySkuId(skuId);
//        //如果已经存在记录，且信息上架时间不为空且>=推送时间,则直接删除掉
//        if(ObjectUtil.isNotNull(validateData)
//                && ObjectUtil.isNotNull(validateData.getStateTime())
//                && DateUtil.compare(validateData.getStateTime(),date)>=0){
//            notifyMsgReadEvent(msg, true);
//            return;
//        }
//        PmsProduct pmsProduct = pmsProductService.getByOutProductId(skuId);
////        log.info("2,商品，skuId={},p={}",skuId,pmsProduct);
//        boolean flag=Boolean.FALSE;
//        if(ObjectUtil.isNotNull(pmsProduct)){
//             //1.判定商品是否已经处于下架状态
//            Integer jdState =Integer.valueOf(getMsg(msg, MessageResult.goodsState)) ;
//            Integer state=pmsProduct.getPublishStatus();
//            if(jdState.equals(state)){
//                //京东状态正好等于商品当前状态不做处理
////                log.info("状态未改变:{}={}",jdState,state);
//                flag=Boolean.TRUE;
//            }else if(JdVopConstant.Product.STATE_NO.equals(jdState)){
//                //修改商品上架状态
////                log.info("修改为下架:{}={},{}",jdState,state,pmsProduct.getId());
//                flag=this.pmsProductService.updatePublishStatusById(state,jdState,pmsProduct.getId());
//            }else if(JdVopConstant.Product.STATE_OK.equals(jdState)){
//                //重新计算价格
////               log.info("重新计算价格:{}={},{}",jdState,state,pmsProduct.getId());
//               flag=this.jdVopProductSoldOutHandler.updateSingleProductPrice(pmsProduct,date);
//            }
//            if(flag){
//                this.pmsProductValidateDataService.saveOrUpdateStateTime(date,skuId);
//            }
//        }else{
//            flag=this.jdVopProductSoldOutHandler.soldOutSingleProduct(Long.valueOf(skuId),date);
//        }
//        notifyMsgReadEvent(msg, flag);
    }


    /**
     * 更新商品详情并推送删除消息
     *
     * @param msg
     */
    public void handleProductDetail(MessageResult msg) {
        DETAIL=++DETAIL;
        log.info("商品详情消息条数============{}=====时间：{}======",DETAIL,new Date());
//        String skuId = getMsg(msg, MessageResult.skuId);
//        Date date=msg.getTime();
//        //验证是否已经记录
//        PmsProductValidateData validateData=this.pmsProductValidateDataService.findOneBySkuId(skuId);
//        //如果已经存在记录，且信息修改时间不为空且>=推送时间,则直接删除掉
//        if(ObjectUtil.isNotNull(validateData)
//                && ObjectUtil.isNotNull(validateData.getInfoTime())
//                && DateUtil.compare(validateData.getInfoTime(),date)>=0){
//            notifyMsgReadEvent(msg, true);
//            return;
//        }
//        PmsProduct pmsProduct = pmsProductService.getByOutProductId(skuId);
//        log.info("2,商品，skuId={},p={}",skuId,pmsProduct);
//        boolean flag;
//        if(ObjectUtil.isNotNull(pmsProduct)){
//            //1.商品存在进行商品信息更新
//            flag=this.jdVopProductSoldOutHandler.dealProductInfoMessageBySkuId(pmsProduct,date);
//        }else{
//            flag=this.jdVopProductSoldOutHandler.soldOutSingleProduct(Long.valueOf(skuId),date);
//        }
//        notifyMsgReadEvent(msg, flag);
    }

    @Override
    public void batchRead(List<MessageResult> msgs) {
        System.out.println("商品状态处理");
        msgs.parallelStream().forEach(this::read);
    }

}
